﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TotalCrytography.Protector;
using System.IO;

namespace TotalCrytography.SystemFile
{
    public class MyFolder
    {
        String _folderName;

        public String FolderName
        {
            get { return _folderName; }
            set { _folderName = value; }
        }
        List<MyFile> _lstFile;

        public List<MyFile> LstFile
        {
            get { return _lstFile; }
            set { _lstFile = value; }
        }

        List<MyFolder> _lstFolder;

        public List<MyFolder> LstFolder
        {
            get { return _lstFolder; }
            set { _lstFolder = value; }
        }

        public MyFolder()
        {
            _folderName = String.Empty;
            _lstFile = new List<MyFile>();
            _lstFolder = new List<MyFolder>();
        }
        public MyFolder(String path)
        {
            _folderName = path.LastIndexOf('\\') > -1 ? getNameFolderFromPath(path) : path;
            DirectoryInfo thisDirInfo = new DirectoryInfo(path);

            _lstFolder = new List<MyFolder>();
            foreach (DirectoryInfo dir in thisDirInfo.GetDirectories())
            {
                _lstFolder.Add(new MyFolder(path+ "\\" + dir) );
            }
            _lstFile = new List<MyFile>();
            foreach (FileInfo file in thisDirInfo.GetFiles())
            {
                _lstFile.Add(new MyFile(path + "\\" +file) );
            }
        }

        public bool Encrypt(String output,Symmetric_Provider sp,int al,int pm, int cm, string pk )
        {
            MakeTempFile(output + ".temp");
            MyFile f = new MyFile(output + ".temp");
            f.Encrypt(output + ".temp", output + ".enc", TypeOfFile.Folder, sp, al, pm, cm, pk);
            File.Delete(output + ".temp");
            return true;
        }

        private bool MakeTempFile(String output)
        {
            try
            {
                //make file from folder
                FileStream fs = new FileStream(output, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                
                //first write folder name
                bw.Write(_folderName);
                //make file from all children folder
                foreach (MyFolder d in LstFolder)
                {
                    //MyFolder tempFolder = new MyFolder(d);
                    d.MakeTempFile(getPathFatherFolder(output) + '\\' + _folderName + '\\' + d.FolderName + ".temp");
                    LstFile.Add(new MyFile(getPathFatherFolder(output) + '\\' + _folderName + '\\' + d.FolderName + ".temp"));
                }
                //join all files to a file
                //write num of files
                bw.Write(LstFile.Count);
                foreach (MyFile f in LstFile)
                {
                    byte[] temp = f.ConvertToBytes();

                    int isFolder = 0;//file
                    foreach( MyFolder fd in LstFolder )
                        if (f.FileName.Remove(f.FileName.LastIndexOf('.')) == fd.FolderName)
                        {
                            isFolder = 1;//folder
                            break;
                        }
                    bw.Write(isFolder);  
                    bw.Write(temp.Length);
                    bw.Write(f.ConvertToBytes());
                }

                bw.Close();
                fs.Close();

                //delte temp file
                foreach (MyFolder d in LstFolder)
                {
                    //MyFolder tempFolder = new MyFolder(d);
                    //d.MakeTempFile(getPathFatherFolder(output) + '\\' + _folderName + '\\' + d.FolderName + ".temp");
                    File.Delete(getPathFatherFolder(output) + '\\' + _folderName + '\\' + d.FolderName + ".temp");
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        private String getNameFolderFromPath(String path)
        {
            return path.Substring(path.LastIndexOf('\\')+1);
        }
        private String getPathFatherFolder(String path)
        {
            return path.Substring(0, path.LastIndexOf('\\'));
        }
    }
}
